package com.jt.tenant.adapter;

import android.content.Context;

import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.jt.tenant.R;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.GetCarModels;


import java.util.List;

/**
 * Created by Administrator on 2016/4/7.
 */
public class CarModelsAdapter extends BaseAdapter {

    private Context context;

    private static boolean isMoved = false;
    private int x = 10000;
    private int y = 10000;
    long mLastTime = 0;
    private long onclickTimes = 0;
    private ItemClick itemClick;

    private  RightSliding rightSliding;

    private List<GetCarModels.CMList> list;

    public CarModelsAdapter(Context context, List<GetCarModels.CMList> list,ItemClick itemClick,RightSliding rightSliding) {

        this.context = context;

        this.list = list;

        this.itemClick = itemClick;

        this.rightSliding = rightSliding;

    }

    @Override
    public int getCount() {

        return list.size();

    }

    @Override
    public Object getItem(int position) {

        return list.get(position);

    }

    @Override
    public long getItemId(int position) {

        return position;

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

        if (convertView == null) {

            convertView = View.inflate(context, R.layout.item_pop_car_models, null);

            JTApplication.ScaleScreenHelper.loadView((ViewGroup) convertView);

            holder = new ViewHolder();

            holder.lin_cm = (LinearLayout)convertView.findViewById(R.id.lin_cm);

            holder.carYear = (TextView) convertView.findViewById(R.id.item_car_models_year);

            holder.carDM = (TextView) convertView.findViewById(R.id.item_car_models_dm);

            holder.carTM = (TextView) convertView.findViewById(R.id.item_car_models_tm);

            holder.carModels = (TextView) convertView.findViewById(R.id.item_car_models_des);

            convertView.setTag(holder);

        } else {

            holder = (ViewHolder) convertView.getTag();

        }
        convertView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub

                switch(event.getAction()) {

                    case MotionEvent.ACTION_DOWN:

                        isMoved = false;

                        break;

                    case MotionEvent.ACTION_UP :

                        break;
                    case MotionEvent.ACTION_MOVE:

                        isMoved = true;

                        int touchX = (int)event.getX();

                        int touchY = (int)event.getY();

                        responsTouchEvent( touchX, touchY );

                        break;
                }


                    return isMoved;


            }


            private void responsTouchEvent(int touchX, int touchY) 	{

                if(x != 10000 && y != 10000) {
                    int absx = touchX - x;
                    int absy = touchY - y;

                    if(absx>40&&Math.abs(absy)<10) {

                        rightSliding.rightSliding();

                    }else if(absx<-40&&Math.abs(absy)<10){

                    }

                }

                x = touchX;
                y = touchY;
            }

        });
        convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                onclickTimes ++;
                //发送鼠标左键功能
                long mCurTime = System.currentTimeMillis();

                //判断是否发生了双击
                if(mLastTime != 0 && mCurTime - mLastTime < 500) {

                    mLastTime = mCurTime - 500;

                }else {

                    isSingClick(onclickTimes);

                    mLastTime = mCurTime;
                }


            }

            //判断是否是单击
            private void isSingClick(final long times) {

                new Thread(){
                    @Override
                    public void run( ) {
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                        if(onclickTimes == times) {

                            itemClick.itemClick(position);
                        }

                    }
                }.start();
            }
        });

        holder.carYear.setText(list.get(position).MODELYEAR);

        holder.carDM.setText(list.get(position).DISPLACEMENT);

        holder.carTM.setText(list.get(position).TRANSMISSION);

        holder.carModels.setText(list.get(position).NAME);

        return convertView;

    }


    class ViewHolder {

        private LinearLayout lin_cm;

        private TextView carYear;

        private TextView carDM;

        private TextView carTM;

        private TextView carModels;

    }

    public interface ItemClick {

        public void itemClick(int pos);

    }


    public interface RightSliding {

        public void rightSliding();

    }
}
